/**
 * 
 */
package ads.engenharia.bl;

import java.sql.SQLException;
import java.util.ArrayList;

import ads.engenharia.dal.CourseDAO;
import ads.engenharia.dal.CourseDisciplineDAO;
import ads.engenharia.entity.Course;
import ads.engenharia.entity.Discipline;
import ads.engenharia.manager.DALManager;

/**
 * @author Lanedraex
 *
 */
public class CourseBO {

	private CourseDAO courseDAO;
	private CourseDisciplineDAO courseDisciplineDAO;
	private DisciplineBO disciplineBO;

	public CourseBO() {

		courseDAO = new CourseDAO();
		courseDisciplineDAO = new CourseDisciplineDAO();
		disciplineBO = new DisciplineBO();
	}

	/**
	 * 
	 * @param course
	 * @throws SQLException
	 */
	public void save(Course course) throws SQLException {

		course = courseDAO.save(course);

	}

	/**
	 * 
	 * @param course
	 * @throws SQLException
	 */
	public void associateCourseDiscipline(Course course) throws SQLException {

		courseDisciplineDAO.delete(course);
		courseDisciplineDAO.save(course);
	}

	/**
	 * 
	 * @param course
	 * @throws SQLException
	 */
	public void delete(Course course) throws SQLException {

		courseDAO.delete(course);

	}

	/**
	 * 
	 * @param course
	 * @throws SQLException
	 */
	public void update(Course course) throws SQLException {

		courseDAO.update(course);
		courseDisciplineDAO.update(course);

	}

	/**
	 * 
	 * @param course
	 * @return
	 * @throws SQLException
	 */
	public Course search(Course course) throws SQLException {

		course = courseDAO.search(course);

		// Procura pelo programa para completar o curso
		course.program(searchCourseProgram(course));

		course.program(fillCourseProgram(course));

		return course;

	}

	/**
	 * 
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Course> searchAll() throws SQLException {

		ArrayList<Course> courses;

		courses = courseDAO.searchAll();

		for (Course c : courses) {
			// Procura pelo programa para completar o curso
			c.program(searchCourseProgram(c));

			c.program(fillCourseProgram(c));

		}

		return courses;

	}

	private ArrayList<Discipline> fillCourseProgram(Course c)
			throws SQLException {
		ArrayList<Discipline> disciplines = new ArrayList<Discipline>();
		for (Discipline d : c.program()) {
			d = DALManager.Instance().disciplineBO().search(d);
			disciplines.add(d);
		}

		return disciplines;
	}

	/**
	 * 
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public Course searchById(int id) throws SQLException {

		Course course;
		course = courseDAO.searchById(id);

		// Procura pelo programa para completar o curso
		course.program(searchCourseProgram(course));

		return course;

	}

	/**
	 * 
	 * @return
	 * @throws SQLException
	 */
	public Course searchByMaxId() throws SQLException {

		Course course;
		course = courseDAO.searchByMaxId();

		// Procura pelo programa para completar o curso
		course.program(searchCourseProgram(course));

		return course;

	}

	/**
	 * 
	 * @param name
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Course> searchByName(String name) throws SQLException {

		ArrayList<Course> courses;

		courses = courseDAO.searchByName(name);

		for (Course c : courses) {
			// Procura pelo programa para completar o curso
			c.program(searchCourseProgram(c));

			c.program(fillCourseProgram(c));
		}

		return courses;

	}

	/**
	 * 
	 * @param duration
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Course> searchByDuration(int duration) throws SQLException {

		ArrayList<Course> courses;

		courses = courseDAO.searchByDuration(duration);

		for (Course c : courses) {
			// Procura pelo programa para completar o curso
			c.program(searchCourseProgram(c));

			c.program(fillCourseProgram(c));
		}

		return courses;

	}

	/**
	 * 
	 * @param course
	 * @return
	 * @throws SQLException
	 */
	private ArrayList<Discipline> searchCourseProgram(Course course)
			throws SQLException {

		return courseDisciplineDAO.searchCourseDisciplines(course);
	}

}
